﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using XYAC.Framework.MonitorTireMaterialOnline.DataExporter;
using XYAC.Framework.MonitorTireMaterialOnline.OracleRemoteDB;

namespace XYAC.Framework.MonitorTireMaterialOnline.Entities
{
    /// <summary>
    /// 胎面报表数据
    /// </summary>
    public class SemiSteelTreadScanReport : ReportEntityBase
    {

        public SemiSteelTreadScanReport()
        {
            LastSyncDate = DateTime.Now;
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public override int ID { get; set; }
        public int UserId { get; set; }
        /// <summary>
        /// 补偿系数
        /// </summary>
        public double CompensationCoefficient { get; set; }
        /// <summary>
        /// 批次号
        /// </summary>
        public int BatchNo { get; set; }

        /// <summary>
        /// 规格号
        /// </summary>
        public string? SpecName { get; set; }
        /// <summary>
        /// 总面积
        /// </summary>
        public double TotalArea { get; set; }
        public double TotalLeftArea { get; set; }
        public double TotalRightArea { get; set; }
        public double MeasureArea { get; set; }
        public double MeasureLeftArea { get; set; }
        public double MeasureRightArea { get; set; }
        /// <summary>
        /// 总宽标准
        /// </summary>
        public double TotalWidth_Stable { get; set; }

        /// <summary>
        /// 总宽扫描
        /// </summary>
        public double TotalWidth_Scan { get; set; }

        /// <summary>
        /// 冠宽对称性
        /// </summary>
        public double TotalWidth_Symmetry { get; set; }
        /// <summary>
        /// 冠宽标准
        /// </summary>
        public double CrownWidth_Stable { get; set; }

        /// <summary>
        /// 冠宽扫描
        /// </summary>
        public double CrownWidth_Scan { get; set; }

        /// <summary>
        /// 冠宽对称性
        /// </summary>
        public double CrownWidth_Symmetry { get; set; }
        /// <summary>
        /// 肩厚标准
        /// </summary>
        public double ShoulderThickness_Stable { get; set; }
        /// <summary>
        /// 肩厚扫描左
        /// </summary>
        public double ShoulderThickness_ScanLeft { get; set; }
        /// <summary>
        /// 肩厚扫描右
        /// </summary>
        public double ShoulderThickness_ScanRight { get; set; }
        /// <summary>
        /// 肩厚对称性
        /// </summary>
        public double ShoulderThickness_Symmetry { get; set; }
        /// <summary>
        /// 冠厚标准
        /// </summary>
        public double CrownThickness_Stable { get; set; }

        /// <summary>
        /// 冠厚扫描
        /// </summary>
        public double CrownThickness_Scan { get; set; }

        /// <summary>
        /// 断面形状
        /// </summary>
        public string? CrossSectionShape { get; set; }
        /// <summary>
        /// 其他点厚度标准
        /// </summary>
        public string? OthoerPointsThickness_Stable { get; set; }
        /// <summary>
        /// 其他点厚度扫描
        /// </summary>
        public string? OthoerPointsThickness_Scan { get; set; }
        /// <summary>
        /// 胎翼端点厚度扫描左
        /// </summary>
        public double BeadWingTipThickness_ScanLeft { get; set; }
        /// <summary>
        /// 胎翼端点厚度扫描右
        /// </summary>
        public double BeadWingTipThickness_ScanRight { get; set; }
        /// <summary>
        /// 胎翼宽度标准
        /// </summary>
        public double BeadWingWidth_Stable { get; set; }
        /// <summary>
        /// 胎翼宽度扫描左
        /// </summary>
        public double BeadWingWidth_ScanLeft { get; set; }
        /// <summary>
        /// 胎翼宽度扫描右
        /// </summary>
        public double BeadWingWidth_ScanRight { get; set; }
        /// <summary>
        /// 胎翼宽度对称性
        /// </summary>
        public double BeadWingWidth_Symmetry { get; set; }
        /// <summary>
        /// 天线胶形状、尺寸
        /// 上宽（mm）2.0±1.0
        /// </summary>
        public double AntennaAdhesiveSpec_TopWidth { get; set; }
        /// <summary>
        /// 天线胶形状、尺寸
        /// 下宽（mm）2.0±1.0
        /// </summary>
        public double AntennaAdhesiveSpec_BottomWidth { get; set; }
        /// <summary>
        /// 天线胶形状、尺寸
        /// 贯通（mm）2.0±1.0
        /// </summary>
        public string? AntennaAdhesiveSpec_Penetration { get; set; }

        /// <summary>
        /// 同步状态
        /// </summary>
        public bool SyncState { get; set; }
        /// <summary>
        /// 最后同步时间
        /// </summary>
        public DateTime? LastSyncDate { get; set; } = DateTime.Now;


        public override YB_PROCESS_STEP ToYB_PROCESS_STEP()
        {
            var dataFile = DataFile.FromSpecVal(this.SpecVal);
            var processStep = new YB_PROCESS_STEP
            {
                // 映射 SemiSteelTreadScanReport 的字段到 RECORD16 至 RECORD43 
                PLAN_ID = this.BatchNo.ToString(),
                PART_NUMBER = this.SpecName,
                RECORD122 = this.TotalArea.ToString("F2"),
                RECORD123 = this.MeasureArea.ToString("F2"),
                RECORD124 = this.TotalLeftArea.ToString("F2"),
                RECORD125 = this.MeasureLeftArea.ToString("F2"),
                RECORD126 = this.TotalRightArea.ToString("F2"),
                RECORD127 = this.MeasureRightArea.ToString("F2"),
                RECORD128 = this.TotalWidth_Stable.ToString("F2"),
                RECORD129 = this.TotalWidth_Scan.ToString("F2"),
                RECORD130 = (this.TotalWidth_Stable / 2).ToString("F2"),
                RECORD131 = Math.Abs(this.SpecVal.IXReference.Value - dataFile.MeasureKeys.FirstOrDefault().Coordinate).ToString("F2"),
                RECORD132 = (this.TotalWidth_Stable / 2).ToString("F2"),
                RECORD133 = Math.Abs(this.SpecVal.IXReference.Value - dataFile.MeasureKeys.LastOrDefault().Coordinate).ToString("F2"),
                RECORD134 = this.CrownWidth_Stable.ToString("F2"),
                RECORD135 = this.CrownWidth_Scan.ToString("F2"),
                RECORD136 = Math.Abs((dataFile.StableKeys.ElementAt(dataFile.StableKeys.Count() - 2).Coordinate - dataFile.StableKeys[1].Coordinate) / 2).ToString("F2"),
                RECORD137 = Math.Abs(this.SpecVal.IXReference.Value - dataFile.MeasureKeys[1].Coordinate).ToString("F2"),
                RECORD138 = Math.Abs((dataFile.StableKeys.ElementAt(dataFile.StableKeys.Count() - 2).Coordinate - dataFile.StableKeys[1].Coordinate) / 2).ToString("F2"),
                RECORD139 = Math.Abs(dataFile.StableKeys.ElementAt(dataFile.MeasureKeys.Count() - 2).Coordinate - this.SpecVal.IXReference.Value).ToString("F2"),
                RECORD140 = dataFile.StableKeys[1].Value.ToString("F2"),
                RECORD141 = dataFile.MeasureKeys[1].Value.ToString("F2"),
                RECORD142 = dataFile.StableKeys.ElementAt(dataFile.StableKeys.Count() - 2).Value.ToString("F2"),
                RECORD143 = dataFile.MeasureKeys.ElementAt(dataFile.MeasureKeys.Count() - 2).Value.ToString("F2"),
                RECORD144 = this.CrownThickness_Stable.ToString("F2"),
                RECORD145 = this.CrownThickness_Scan.ToString("F2"),
                RECORD146 = dataFile.StableKeys[0].Value.ToString("F2"),
                RECORD147 = dataFile.MeasureKeys[0].Value.ToString("F2"),
                RECORD148 = dataFile.StableKeys.LastOrDefault().Value.ToString("F2"),
                RECORD149 = dataFile.MeasureKeys.LastOrDefault().Value.ToString("F2"),
                RECORD150 = Math.Abs(dataFile.StableKeys[1].Coordinate - dataFile.StableKeys[0].Coordinate).ToString("F2"),
                RECORD151 = Math.Abs(dataFile.MeasureKeys[1].Coordinate - dataFile.MeasureKeys[0].Coordinate).ToString("F2"),

                RECORD152 = Math.Abs(dataFile.StableKeys.ElementAt(dataFile.StableKeys.Count() - 2).Coordinate - dataFile.StableKeys.LastOrDefault().Coordinate).ToString("F2"),
                RECORD153 = Math.Abs(dataFile.MeasureKeys.ElementAt(dataFile.MeasureKeys.Count() - 2).Coordinate - dataFile.MeasureKeys.LastOrDefault().Coordinate).ToString("F2"),
                RECORD154 = this.SpecVal.TestResult,
                RECORD156 = this.SpecVal.IntervalWidth?.ToString("F2"),

                RECORD160 = this.AreaPencent.ToString(),
                RECORD162 = this.CrownWidth_Symmetry.ToString("F2"),
                RECORD163 = this.ShoulderThickness_Symmetry.ToString("F2"),
                RECORD165 = this.BeadWingWidth_Symmetry.ToString("F2"),

                RECORD201 = this.SpecVal.AlarmPoints

            };
            this.UpdateProcessStep(processStep, dataFile);
            return processStep;
        }
    }
}
